package com.wen.mall.order.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.http.HttpRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: TODO
 * @created: IntelliJ IDEA
 * @author: QWQ
 * @createTime: 2022/10/15 14:26
 **/
@Configuration
public class FeignConfig {

    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor(){
        return requestTemplate -> {
            //1.RequestContextHolder拿到请求数据
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            String cookie = request.getHeader("Cookie");
            //同步Cookie
            requestTemplate.header("Cookie", cookie);
        };
    }

}
